package com.epoint.mobileoa.ui.contacts;

import java.util.HashMap;
import java.util.Map;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.ConfigKey;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.SynchroSingleContactsTask;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.MobileOperation;
import com.epoint.mobileoa.tools.PhoneHelp;
import com.epoint.mobileoa.tools.StringHelp;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author liyc
 * @time 2012-1-10 上午9:34:07
 * @annotation
 */
public class ContactsDetailView extends SuperView {

	private RelativeLayout cd2_rl_name, cd2_rl_sex, cd2_rl_title, cd2_rl_dep,
			cd2_rl_officetel, cd2_rl_ntx, cd2_rl_mobile, cd2_rl_hometel,
			cd2_rl_shortnum, cd2_rl_qqnum, cd2_rl_email, cd2_rl_remark;
	private TextView cd2_rl_name_tv, cd2_rl_sex_tv, cd2_rl_title_tv,
			cd2_rl_dep_tv, cd2_rl_officetel_tv, cd2_rl_ntx_tv,
			cd2_rl_mobile_tv, cd2_rl_hometel_tv, cd2_rl_shortnum_tv,
			cd2_rl_qqnum_tv, cd2_rl_email_tv, cd2_rl_remark_tv;
	private Button cd2_bgimg, cd2_mobile_msg_img, cd2_mobile_call_img,
			cd2_dx_msg_img, cd2_dx_call_img, cd2_home_call, cd2_bt_photoscan,
			cd2_bt_ds;
	private Map<String, String> map;
	private String userguid;
	private ProgressDialog m_Dialog;
	public static final int SynchroSingleContacts = 1;

	@Override
	public void initControls() {

		Intent intent = getIntent();
		userguid = intent.getStringExtra("userguid");
		map = DBHelp.getUserDetailInfo(this, userguid);
		cd2_rl_name = (RelativeLayout) findViewById(R.id.cd2_rl_name);
		cd2_rl_sex = (RelativeLayout) findViewById(R.id.cd2_rl_sex);
		cd2_rl_title = (RelativeLayout) findViewById(R.id.cd2_rl_title);
		cd2_rl_dep = (RelativeLayout) findViewById(R.id.cd2_rl_dep);
		cd2_rl_officetel = (RelativeLayout) findViewById(R.id.cd2_rl_officetel);
		cd2_rl_ntx = (RelativeLayout) findViewById(R.id.cd2_rl_ntx);
		cd2_rl_mobile = (RelativeLayout) findViewById(R.id.cd2_rl_mobile);
		cd2_rl_hometel = (RelativeLayout) findViewById(R.id.cd2_rl_hometel);
		cd2_rl_shortnum = (RelativeLayout) findViewById(R.id.cd2_rl_shortnum);
		cd2_rl_qqnum = (RelativeLayout) findViewById(R.id.cd2_rl_qqnum);
		cd2_rl_email = (RelativeLayout) findViewById(R.id.cd2_rl_email);
		cd2_rl_remark = (RelativeLayout) findViewById(R.id.cd2_rl_remark);

		cd2_rl_name_tv = (TextView) findViewById(R.id.cd2_rl_name_tv);
		cd2_rl_sex_tv = (TextView) findViewById(R.id.cd2_rl_sex_tv);
		cd2_rl_title_tv = (TextView) findViewById(R.id.cd2_rl_title_tv);
		cd2_rl_dep_tv = (TextView) findViewById(R.id.cd2_rl_dep_tv);
		cd2_rl_officetel_tv = (TextView) findViewById(R.id.cd2_rl_officetel_tv);
		cd2_rl_ntx_tv = (TextView) findViewById(R.id.cd2_rl_ntx_tv);
		cd2_rl_mobile_tv = (TextView) findViewById(R.id.cd2_rl_mobile_tv);
		cd2_rl_hometel_tv = (TextView) findViewById(R.id.cd2_rl_hometel_tv);
		cd2_rl_shortnum_tv = (TextView) findViewById(R.id.cd2_rl_shortnum_tv);
		cd2_rl_qqnum_tv = (TextView) findViewById(R.id.cd2_rl_qqnum_tv);
		cd2_rl_email_tv = (TextView) findViewById(R.id.cd2_rl_email_tv);
		cd2_rl_remark_tv = (TextView) findViewById(R.id.cd2_rl_remark_tv);
		cd2_bt_ds = (Button) findViewById(R.id.cd2_bt_ds);
		top_title.setText(map.get("name"));
		cd2_rl_name_tv.setText(map.get("name"));
		cd2_rl_sex_tv.setText(map.get("sex"));
		cd2_rl_hometel_tv.setText(map.get("telephonehome"));
		cd2_rl_officetel_tv.setText(map.get("officephone"));
		cd2_rl_email_tv.setText(map.get("email"));
		cd2_rl_dep_tv.setText(map.get("deptname"));
		cd2_rl_title_tv.setText(map.get("title"));
		cd2_rl_remark_tv.setText(map.get("remark"));
		cd2_rl_shortnum_tv.setText(map.get("shortmobile"));
		cd2_rl_ntx_tv.setText(map.get("ntxextnumber"));
		cd2_rl_qqnum_tv.setText(map.get("qqnumber"));
		cd2_rl_mobile_tv.setText(map.get("mobilephone"));

		setTextViewFontColor(cd2_rl_name_tv);
		setTextViewFontColor(cd2_rl_sex_tv);
		setTextViewFontColor(cd2_rl_hometel_tv);
		setTextViewFontColor(cd2_rl_officetel_tv);
		setTextViewFontColor(cd2_rl_email_tv);
		setTextViewFontColor(cd2_rl_dep_tv);
		setTextViewFontColor(cd2_rl_title_tv);
		setTextViewFontColor(cd2_rl_remark_tv);
		setTextViewFontColor(cd2_rl_shortnum_tv);
		setTextViewFontColor(cd2_rl_ntx_tv);
		setTextViewFontColor(cd2_rl_qqnum_tv);
		setTextViewFontColor(cd2_rl_mobile_tv);

		cd2_bgimg = (Button) findViewById(R.id.cd2_bgimg);
		cd2_mobile_msg_img = (Button) findViewById(R.id.cd2_mobile_msg_img);
		cd2_mobile_call_img = (Button) findViewById(R.id.cd2_mobile_call_img);
		cd2_dx_msg_img = (Button) findViewById(R.id.cd2_dx_msg_img);
		cd2_dx_call_img = (Button) findViewById(R.id.cd2_dx_call_img);
		cd2_home_call = (Button) findViewById(R.id.cd2_home_call);

		cd2_bt_photoscan = (Button) findViewById(R.id.cd2_bt_photoscan);

		// 拨打办公电话
		cd2_bgimg.setOnClickListener(this);

		// 拨打手机
		cd2_mobile_call_img.setOnClickListener(this);

		cd2_home_call.setOnClickListener(this);

		cd2_dx_call_img.setOnClickListener(this);

		cd2_dx_msg_img.setOnClickListener(this);

		cd2_mobile_msg_img.setOnClickListener(this);

		cd2_bt_photoscan.setOnClickListener(this);

		cd2_bt_ds.setOnClickListener(this);
		if (DBHelp.getConfigValue(this, ConfigKey.isTuojiLogin).equals("1")) {
			cd2_bt_ds.setVisibility(View.INVISIBLE);
		}
		try {
			if (map.get("officephone").equals("null")
					|| map.get("officephone").length() == 0) {
				cd2_bgimg.setVisibility(View.GONE);
			}
		} catch (Exception e) {
			cd2_bgimg.setVisibility(View.GONE);
		}
		try {
			if (map.get("mobilephone").equals("null")
					|| map.get("mobilephone").length() == 0
					|| map.get("mobilephone") == null) {
				cd2_mobile_msg_img.setVisibility(View.GONE);
				cd2_mobile_call_img.setVisibility(View.GONE);
			}
		} catch (Exception e) {
			cd2_mobile_msg_img.setVisibility(View.GONE);
			cd2_mobile_call_img.setVisibility(View.GONE);
		}
		try {
			if (map.get("shortmobile").equals("null")
					|| map.get("shortmobile").length() == 0
					|| map.get("shortmobile") == null) {
				cd2_dx_msg_img.setVisibility(View.GONE);
				cd2_dx_call_img.setVisibility(View.GONE);
			}
		} catch (Exception e) {
			cd2_dx_msg_img.setVisibility(View.GONE);
			cd2_dx_call_img.setVisibility(View.GONE);
		}

		try {
			if (map.get("telephonehome").equals("null")
					|| map.get("telephonehome").length() == 0
					|| map.get("telephonehome") == null) {
				cd2_home_call.setVisibility(View.GONE);
			}
		} catch (Exception e) {
			cd2_home_call.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == cd2_bgimg.getId()) {
			// 加上分机号前缀051258188000
			MobileOperation.Call(this, map.get("officephone"));
		} else if (v.getId() == cd2_mobile_call_img.getId()) {
			MobileOperation.Call(this, map.get("mobilephone"));
		} else if (v.getId() == cd2_home_call.getId()) {
			MobileOperation.Call(this, map.get("telephonehome"));
		} else if (v.getId() == cd2_dx_call_img.getId()) {
			MobileOperation.Call(this, map.get("shortmobile"));
		} else if (v.getId() == cd2_dx_msg_img.getId()) {
			sendMSG(map.get("shortmobile"));
		} else if (v.getId() == cd2_mobile_msg_img.getId()) {
			sendMSG(map.get("mobilephone"));
		} else if (v.getId() == cd2_bt_photoscan.getId()) {
			Intent intent = new Intent(this, ContactPhotoScan.class);
			intent.putExtra("userguid", userguid);
			intent.putExtra("name", map.get("name"));
			startActivity(intent);
		} else if (v.getId() == cd2_bt_ds.getId()) {
			m_Dialog = new ProgressDialog(this);
			m_Dialog.setMessage("正在同步该条通讯录...");
			m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			m_Dialog.setCancelable(true);
			m_Dialog.show();
			HashMap param = new HashMap();
			param.put("userguid", userguid);
			Task task = new SynchroSingleContactsTask(this, param);
			MainService.addTask(task);
		}
	}

	public void sendMSG(String phonenum) {
		PhoneHelp.sendMsgUsePhoneSelf(
				this,
				phonenum,
				map.get("name") + "你好,我是"
						+ DBHelp.getUserOuname(this, fp.getUserguid())
						+ fp.getDisplayname() + ",");
	}

	@Override
	public void refresh(Object... param) {
		if (param[0].toString().equals(MainService.NORMAL)) {
			String bs = (String) param[1];
			if (bs.equals("none")) {

			} else {
				Map<String, String> map = new HashMap<String, String>();
				map.put("sex", StringHelp.getXMLAtt(bs, "sex"));
				map.put("title", StringHelp.getXMLAtt(bs, "title"));
				map.put("telephoneoffice",
						StringHelp.getXMLAtt(bs, "telephoneoffice"));
				map.put("ntxextnumber",
						StringHelp.getXMLAtt(bs, "ntxextnumber"));
				map.put("shortmobile", StringHelp.getXMLAtt(bs, "shortmobile"));
				map.put("telephonehome",
						StringHelp.getXMLAtt(bs, "telephonehome"));
				map.put("qqnumber", StringHelp.getXMLAtt(bs, "qqnumber"));
				map.put("email", StringHelp.getXMLAtt(bs, "email"));
				map.put("description", StringHelp.getXMLAtt(bs, "description"));
				map.put("displayname", StringHelp.getXMLAtt(bs, "displayname"));
				map.put("mobile", StringHelp.getXMLAtt(bs, "mobile"));
				DBHelp.updateUser(this, userguid, map);

				top_title.setText(map.get("displayname"));
				cd2_rl_name_tv.setText(map.get("displayname"));
				cd2_rl_sex_tv.setText(map.get("sex"));
				cd2_rl_hometel_tv.setText(map.get("telephonehome"));
				cd2_rl_officetel_tv.setText(map.get("telephoneoffice"));
				cd2_rl_email_tv.setText(map.get("email"));
				cd2_rl_title_tv.setText(map.get("title"));
				cd2_rl_remark_tv.setText(map.get("description"));
				cd2_rl_shortnum_tv.setText(map.get("shortmobile"));
				cd2_rl_ntx_tv.setText(map.get("ntxextnumber"));
				cd2_rl_qqnum_tv.setText(map.get("qqnumber"));
				cd2_rl_mobile_tv.setText(map.get("mobile"));
			}
		}
		m_Dialog.dismiss();
	}

	@Override
	public int getLayoutInt() {
		return R.layout.contactsdetailview2;
	}


	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}
}
